﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using EnvDTE;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Shell.Interop;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.VsSDK.IntegrationTestLibrary;
using Microsoft.VSSDK.Tools.VsIdeTesting;

namespace SolutionSettings_IntegrationTests {
    /// <summary>
    /// Integration test for package validation
    /// </summary>
    [TestClass]
    public class PackageTest {
        private delegate void ThreadInvoker();

        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext {
            get {
                return testContextInstance;
            }
            set {
                testContextInstance = value;
            }
        }

        [TestMethod]
        [HostType("VS IDE")]
        public void PackageLoadTest() {
            UIThreadInvoker.Invoke((ThreadInvoker)delegate() {
                //Get the Shell Service
                IVsShell shellService = VsIdeTestHostContext.ServiceProvider.GetService(typeof(SVsShell)) as IVsShell;
                Assert.IsNotNull(shellService);

                //Validate package is loaded on startup
                IVsPackage package;
                Guid packageGuid = new Guid(SolutionSettings.GuidList.guidSolutionSettingsPkgString);
                Assert.AreEqual(0, shellService.IsPackageLoaded(ref packageGuid, out package));
                Assert.IsNotNull(package, "Package failed to load");
            });
        }

        [TestMethod]
        [HostType("VS IDE")]
        public void SolutionLoadTest_NoSettings() {
            UIThreadInvoker.Invoke((ThreadInvoker)delegate() {
                var dte = VsIdeTestHostContext.Dte;
                var utils = new TestUtils();

                utils.CreateEmptySolution(Environment.CurrentDirectory, "SolutionLoadTest");
                var filename = dte.Solution.FileName;
                utils.CloseCurrentSolution(__VSSLNSAVEOPTIONS.SLNSAVEOPT_ForceSave);

                VsIdeTestHostContext.Dte.Solution.Open(filename);
            });
        }
    }
}
